package com.huluobo.a2203kotlindouyindemo.adapter

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.huluobo.a2203kotlindouyindemo.R
import com.huluobo.base.bean.MessageEntity

/**
 *  Created by LC on 2024/7/9.
 */
class MessageAdapter(list: MutableList<MessageEntity>) :
    BaseQuickAdapter<MessageEntity, BaseViewHolder>(R.layout.item_chat_room, list) {
    override fun convert(holder: BaseViewHolder, item: MessageEntity) {
        val msg = "${item.username}:${item.content}"

        //<span color = "red">123</span>
        val builder = SpannableStringBuilder(msg)

        val blue = ForegroundColorSpan(Color.BLUE)
        val yellow = ForegroundColorSpan(Color.YELLOW)

        //张三:你好  SPAN_EXCLUSIVE_INCLUSIVE 前后是否包含元素
        builder.setSpan(blue, 0, item.username.length + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        builder.setSpan(yellow, item.username.length + 1, msg.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)

        holder.setText(R.id.item_chat_room_content_tv, builder)
    }
}